﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
using System.Web.UI.WebControls;
using System.Text;
using aucn.web.au.code;

namespace aucn.web
{
    public partial class VerticalMenu : System.Web.UI.UserControl
    {
        string subMenu = string.Empty;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            string panel = "pn" + Request.QueryString["ref"];
            try
            {
                this.FindControl(panel).Visible = true;
            }
            catch { }
        }

        //protected void DisplayMasterList()
        //{
        //    Repeater1.Visible = true;

        //    if (Cache["MASTER_LIST"] == null)
        //    {
        //        XmlDocument doc = new XmlDocument();
        //        doc.Load(Server.MapPath("~/App_Data/master.xml"));
        //        XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
        //        xm.AddNamespace("x", "http://tempuri.org/master.xsd");
        //        XmlNodeList nodeList = doc.DocumentElement.SelectNodes("x:department", xm);
        //        Dictionary<string, string> deptList = new Dictionary<string, string>();
        //        foreach (XmlNode deptNode in nodeList)
        //        {
        //            deptList.Add(deptNode.Attributes["name"].Value, deptNode.SelectSingleNode("x:name", xm).InnerText);
        //        }

        //        Cache["MASTER_LIST"] = deptList;
        //    }
        //    Repeater1.DataSource = (Dictionary<string, string>)Cache["MASTER_LIST"];
        //    Repeater1.DataBind();
        //}

        protected void DisplayUndergradList()
        {
            if (Cache["DEPTLIST"] == null)
            {
                // Load departments
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(Server.MapPath("~/App_Data/subjects.xml"));
                    XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
                    xm.AddNamespace("ab", "http://tempuri.org/subject.xsd");
                    XmlNodeList nodeList = doc.DocumentElement.SelectNodes("ab:department", xm);
                    //KeyValuePair<string, string> list = new KeyValuePair<string, string>();
                    List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                    StringBuilder html = new StringBuilder();
                    foreach (XmlNode node in nodeList)
                    {
                        html.Append(string.Format("<li><a href='/au/subject.aspx?d={0}'>{1}</a></li>", node.Attributes["name"].Value, node.SelectSingleNode("ab:chname", xm).InnerText));
                        //list.Add(new KeyValuePair<string,string>(node.Attributes["name"].Value, node.SelectSingleNode("ab:name", xm).InnerText));
                    }
                    Cache["DEPTLIST"] = html.ToString();
                    ltrDepts.Text = (string)Cache["DEPTLIST"];
                }
                catch
                {
                }
            }

            else
            {
                ltrDepts.Text = (string)Cache["DEPTLIST"];
            }
        }

        public string SubMenu 
        { 
            get { return this.subMenu; } 
            set
            {
                this.subMenu = value;

                try
                {
                    HyperLink l = (HyperLink)this.FindControl("lnk" + value);
                    l.CssClass += " current";
                }
                catch { }

                try
                {
                    this.FindControl("pn" + value).Visible = true;
                }
                catch { }

                if (value == "Undergrad")
                {
                    this.DisplayUndergradList();
                }
                else if (value == "Postgrad")
                {
                    //this.DisplayMasterList();
                }
                else if (value == "Facility")
                {
                    this.DisplayFacilityList();
                }
            } 
        }

        protected void DisplayFacilityList()
        {
            List<Facility> bind = null;
            if (Cache["FACLIST"] == null)
            {
                List<Facility> facList = new List<Facility>();
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/App_Data/facility.xml"));
                XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
                xm.AddNamespace("x", "http://tempuri.org/facility.xsd");

                XmlNodeList facNodeList = doc.DocumentElement.SelectNodes("x:Facility", xm);
                foreach (XmlNode facNode in facNodeList)
                {
                    facList.Add(new Facility(facNode.Attributes["Name"].Value, facNode.SelectSingleNode("x:FacilityName", xm).InnerText, string.Empty));
                }

                Cache["FACLIST"] = facList;
            }

            bind = (List<Facility>)Cache["FACLIST"];
            rptrFac.DataSource = bind;
            rptrFac.DataBind();
        }
    }
}